package com.xinchuang.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.comment.R;
import com.xinchuang.common.KeyWordSystem;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.constant.ImageTypeConstant;
import com.xinchuang.entity.common.ImgTargetTable;
import com.xinchuang.entity.image.ImageCut;
import com.xinchuang.entity.targetSystem.target.IndexRelationTable;
import com.xinchuang.service.StillService;
import com.xinchuang.service.common.ImgTargetTableService;
import com.xinchuang.service.image.ImageCutService;
import com.xinchuang.util.FileElseUtil;
import com.xinchuang.util.LngLatUtil;
import com.xinchuang.util.RedisUtil;
import com.xinchuang.utils.CheckUtil;
import com.xinchuang.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.awt.geom.Point2D;
import java.util.*;
import java.text.SimpleDateFormat;

import com.xinchuang.utils.DateUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;

@Service
@RequiredArgsConstructor
public class StillServiceImpl implements StillService {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final ImageCutService imageCutService;

    private final ImgTargetTableService imgTargetTableService;

    private final KeyWordSystem keyWordSystem;

    @Override
    public Result imgList(JSONObject jsonObject) {
        JSONArray positions = jsonObject.getJSONArray("positions");

        if (CollectionUtil.isEmpty(positions)) {
            return ResultUtil.error(500, "请选择需要查询影像的范围");
        }

        List<Map<String, Object>> resultList = new ArrayList<>();

        //获取当前上传的影像列表
        List<ImgTargetTable> imgTargetTables = imgTargetTableService.list();

        if (CollectionUtil.isNotEmpty(imgTargetTables)) {
            //封装多边形
            List<Point2D.Double> polygon = new ArrayList<>();
            for (int i = 0; i < positions.size(); i++) {
                JSONArray jsonArray = positions.getJSONArray(i);
                double lon = jsonArray.getDouble(0);
                double lat = jsonArray.getDouble(1);
                Point2D.Double p = new Point2D.Double();
                p.setLocation(lon, lat);
                polygon.add(p);
            }

            for (ImgTargetTable img : imgTargetTables) {
                if (StringUtils.isNotBlank(img.getLon()) && StringUtils.isNotBlank(img.getLat())) {
                    Point2D.Double point = new Point2D.Double();
                    point.setLocation(Double.parseDouble(img.getLon()), Double.parseDouble(img.getLat()));
                    List<Point2D.Double> pn = new ArrayList<>();
                    pn.addAll(polygon);
                    if (LngLatUtil.areaCheck(point, pn)) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("name", img.getName());
                        map.put("position", Arrays.asList(point.getX(), point.getY(), 0D));
                        map.put("icon", keyWordSystem.getSERVER_URL() + img.getThumbnail());
                        map.put("typeName", img.getType());
                        map.put("image", keyWordSystem.getSERVER_URL() + img.getImgValue());
                        map.put("rectangle", FileElseUtil.getObjectToList(img.getImgPosition()));
                        resultList.add(map);
                    }
                }
            }

        }

        return ResultUtil.success(resultList);
    }

    @Override
    public String img(JSONObject jsonObject) {
        //jsonobject解析获取影像标识
        String imgId = jsonObject.getString("imgId");
        ImageCut imageCut = imageCutService.lambdaQuery().eq(ImageCut::getId, imgId).one();
        if (ObjectUtil.isNotEmpty(imageCut)) {
            JSONObject json = new JSONObject();
            json.put("imgUrl", imageCut.getImage());
            json.put("centerPosition", Arrays.asList(Double.valueOf(imageCut.getLon()), Double.valueOf(imageCut.getLat()), "0.0"));
            json.put("minLon", imageCut.getMinLon());
            json.put("minLat", imageCut.getMinLat());
            json.put("maxLon", imageCut.getMaxLon());
            json.put("maxLat", imageCut.getMaxLat());
            return JSON.toJSONString(R.data(json));
        }
        return JSON.toJSONString(R.success("无数据"));
    }

    @Override
    public String tarPlotList() {
        if (1 == 1
        ) {
            return "{\"msg\":\"操作成功\",\"code\":200,\"data\":[{\"plotList\":\"标绘字符串\"}],\"success\":true}";
        }
        if (1 == 1
        ) {
            return "";
        }
        return JSON.toJSONString(R.success("无数据"));
    }

}
